package com.sm.controller;

import com.sm.domain.dto.LoginDTO;
import com.sm.domain.dto.Result;
import com.sm.domain.dto.UserDTO;
import com.sm.domain.po.User;
import com.sm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

	@Autowired
	private UserService userService;

	@PostMapping("/register")
	public Result userRegister(@RequestBody User user) {
		return userService.register(user);
	}

	@PostMapping("/login")
	public Result userLogin(@RequestBody LoginDTO loginDTO) {
		return userService.login(loginDTO);
	}

	@GetMapping("/users")
	public Result listUsers(@RequestParam(defaultValue = "1") Integer pageNo, @RequestParam(defaultValue = "5") Integer pageSize) {
		return userService.listUsers(pageNo, pageSize);
	}

	@GetMapping("/{targetUserId}")
	public Result getUserInfo(@PathVariable Long targetUserId) {
		return userService.getUserInfo(targetUserId);
	}

	@PutMapping("/{userId}")
	public Result updateUserInfo(@PathVariable Long userId, @RequestBody UserDTO userDto) {
		return userService.updateUserInfo(userId, userDto);
	}

	@PostMapping("/reset-password/{userId}")
	public Result resetPassword(@PathVariable Long userId) {
		return userService.resetPassword(userId);
	}
}
